package tq.com.tqteacher.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.systools.system.StringHelper;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.propertyEnum.TQModify;

public class TQNewPingJiangPublish extends TQNewPublish {
    private ArrayList<TQAtachModel> mAtachModels = new ArrayList<>();

    public TQNewPingJiangPublish(){}

    public void onPulishComplete(INetCallback pNetCallback){
        HashMap<String, Object> param = new HashMap<>();
        param.put("category", Integer.valueOf(getType()));
        param.put("title", getName());
        param.put("desc", getDesc());
        param.put("grade", Integer.valueOf(getGrade()));
        param.put("chapter", Integer.valueOf(getParentId()));
        param.put("access_type", Integer.valueOf(getShow()));

        TQNetInterface.newPublish(param, pNetCallback);
    }

    public void onModifyBaseInfo(INetCallback pNetCallback){
        HashMap<TQModify.MaterialBaseInfo, Object> param = new HashMap<>();
        param.put(TQModify.MaterialBaseInfo.title, getName());
        param.put(TQModify.MaterialBaseInfo.desc, getDesc());
        param.put(TQModify.MaterialBaseInfo.access_type, String.valueOf(getShow()));
        param.put(TQModify.MaterialBaseInfo.grade, String.valueOf(getGrade()));

        TQNetInterface.modifyMaterialInfo(getSeq_id(), param, pNetCallback);
    }

    public void refreshAttach(JSONArray pObject){
        if(pObject == null || pObject.length() == 0) return;

        mAtachModels.clear();
        for (int i  =0; i < pObject.length(); i++){
            try {
                JSONObject lObject = pObject.getJSONObject(i);
                TQAtachModel lAtachModel = new TQAtachModel();
                lAtachModel.fromJson(lObject);

                mAtachModels.add(lAtachModel);
            }catch (JSONException pE){
                pE.printStackTrace();
            }
        }

    }

    public ArrayList<TQAtachModel> getAtachModels() {
        return mAtachModels;
    }


    public void appendAttach(TQAtachModel pModel){
        mAtachModels.add( pModel);
    }
    public void delAttach(TQAtachModel pModel){
        mAtachModels.remove(pModel);
    }

    public int getCropCount(){
        return mAtachModels.size();
    }

    public int getCaptureCount(){
        int count = 0;
        for (TQAtachModel model:
             mAtachModels) {
            if(!StringHelper.isEmpty(model.getFirstFile())){
                count++;
            }
        }
        return count;
    }
}
